<template>
  <div>
    <el-card style="width: 220px;margin: 20px;height: 180px;float: left;" bodyStyle="height: 170px;padding:10px" shadow="hover">
      <i class="el-icon-circle-plus-outline"  @click="dialogFormVisible = true"></i>
    </el-card>
    <el-dialog
      title="添加/修改荣誉展览"
      :visible.sync="dialogFormVisible"
      @close="clear">
      <el-form v-model="form" style="text-align: left" ref="dataForm">
        <el-form-item label="标题" :label-width="formLabelWidth" prop="">
          <el-input type="textarea" v-model="form.hwTitle" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="介绍" :label-width="formLabelWidth" prop="">
          <el-input type="textarea" v-model="form.hwIntroduction" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth" prop="">
          <el-input v-model="form.hwPicture" autocomplete="off" placeholder="图片 URL"></el-input>
          <img-upload @onUpload="uploadImg" ref="imgUpload"></img-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import imgUpload from '../../common/ImgUpload'
import api from '../../../apis/admin/honor'
import {Message} from 'element-ui'
export default {
  name: 'HonorEdit',
  components: {imgUpload},
  data () {
    return {
      form: {
        id: '',
        hwTitle: '',
        hwPicture: '',
        hwIntroduction: '',
        hwPushTime: '',
        hwStar: ''
      },
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  methods: {
    clear () {
      this.form = {
        id: '',
        hwTitle: '',
        hwPicture: '',
        hwIntroduction: '',
        hwPushTime: '',
        hwStar: ''
      }
    },
    uploadImg (url) {
      // 更改对象数组的内部的对象不会触发vue对dom的监听，无法通过双向绑定实时修改数据，需要通过$set方法触发
      // Vue.nextTick(callback) 基于更新后的 DOM 状态来做点什么
      this.$set(this.form, 'hwPicture', url)
    },
    async onSubmit () {
      let result
      if (this.form.id) {
        result = await api.updateHonor(this.form)
      } else {
        result = await api.addHonor(this.form)
      }
      if (result.code === 200) {
        this.dialogFormVisible = false
        this.$emit('onSubmit')
        Message.success(result.msg)
      } else {
        Message.error(result.msg)
      }
    }
  }
}
</script>

<style scoped>
.el-icon-circle-plus-outline {
  margin: 40px 0 0 40px;
  font-size: 100px;
  float: left;
  cursor: pointer;
}
</style>
